package esay.array;

/**
 * 翻转数组
 * @author ruizhou
 * @date 24.2.2018
 */
public class RotateArray {
    private static void rotate(int[] nums, int k) {
        int len = nums.length;
        int reversal_len = k > len ? k % len : k;

        //将整个数组翻转
        reversal(nums, 0, len -1);
        //前半部分翻转
        reversal(nums, 0, reversal_len -1);
        //后半部分翻转
        reversal(nums, reversal_len, len -1);
    }

    /**
     * 逆转数组start-end的数字
     * @param nums
     * @param start
     * @param end
     */
    private static void reversal(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end --;
        }
    }

    public static void main(String[] args) {
        int [] nums = {1,2,3,4,5,6,7};
        rotate(nums, 3);
    }
}
